#include "hash.h"
#include <stdio.h>
#include <stdlib.h>
#include "object.h"

node ** CreateValueHash(void)
{
  node ** tmp = (node **) malloc (sizeof(node *) * MAX_HASH_VALUE);
  return tmp;
}

static int hashvalue(int a)
{
  int temp = a % MAX_HASH_VALUE;
  if(temp<0) temp = temp * -1;
  return temp;
}

void * AddValueToHash(int a, node ** array)
{
  int i;
  node * interpolator;

  if(array ==NULL)
    return NULL;
  
  i = hashvalue(a);
  
  if(array[i] == NULL)
    {
      array[i] = (node *) malloc (sizeof(node));
      array[i]->integer = a;
      array[i]->valor = NewInt(a);
      array[i]->next = NULL;
      return array[i]->valor;
    }
  else
    {
    interpolator = array[i];		
	while(interpolator->next != NULL)
	{
	  if(interpolator->integer == a)
	    {
	      return interpolator->valor;
	    }
	  interpolator = interpolator->next;
	}
    if(interpolator->integer == a)
	{
	 return interpolator->valor;
	}
      interpolator->next = (node *) malloc (sizeof(node));
      interpolator->next->integer = a;
      interpolator->next->valor = NewInt(a);
      interpolator->next->next = NULL;
      return interpolator->next->valor;
    }
}
  
void DeleteValueHash(node ** array)
{
  int i;
  node * interpolator;
  node * tmp;
  for(i=0;i<MAX_HASH_VALUE; i++)
    {
      if(array[i] != NULL)
	{
	  interpolator = array[i];
	  while(interpolator != NULL)
	    {
	      tmp = interpolator->next;
	      free(interpolator->valor);
	      free(interpolator);
	      interpolator = tmp;
	    }
	}
    }
}
